// import { transform } from '@babel/standalone'
import { transformFromAstAsync } from '@babel/core'
import jsx from '@vue/babel-plugin-jsx'
// import hashId from 'hash-sum'
// @ts-ignore (Could not find a declaration file for module '@babel/plugin-transform-modules-commonjs')
import babelPluginTransformModulesCommonjs from '@babel/plugin-transform-modules-commonjs'
//@ts-ignore
const targetBrowserBabelPlugins = { ...(typeof ___targetBrowserBabelPlugins !== 'undefined' ? ___targetBrowserBabelPlugins : {}) };
//
// export async function babelTransform(ast: any, babel:any, source:string, moduleSourceType: boolean, additionalBabelPlugins: any = {}, devMode: boolean,log: any){
//   if (babel){
//     const result = babel.transformFromAst(ast,source, {
//       plugins: [
//         jsx,
//         // 'transform-modules-commonjs',
//         ...moduleSourceType ? [ babelPluginTransformModulesCommonjs ] : [], // https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs#options
//         ...Object.values(targetBrowserBabelPlugins),
//         ...additionalBabelPlugins !== undefined ? Object.values(additionalBabelPlugins) : [],
//       ],
//       babelrc: false,
//       configFile: false,
//       highlightCode: false,
//       compact: !devMode, // doc: All optional newlines and whitespace will be omitted when generating code in compact mode.
//       comments: devMode,
//       retainLines: devMode,
//       //envName: devMode ? 'development' : 'production', see 'process.env.BABEL_ENV': JSON.stringify(mode),
//       //minified,
//       sourceType: moduleSourceType ? 'module' : 'script',
//     })
//     if (result.code){
//       return result.code;
//     }else{
//       const msg = `unable to transform script`;
//       log?.('error', msg);
//       throw new Error(msg)
//     }
//   }else{
//     return source;
//   }
// }
//
// export async function transformJSX(ast: any,source: string,moduleSourceType:boolean, additionalBabelPlugins: any = {}, devMode: boolean,log: any) {
//   let code = source
//   // @ts-ignore
//   const babel: any = window.Babel || null;
//   if (babel){
//     code = await babelTransform(ast,babel,source, moduleSourceType, additionalBabelPlugins, devMode, log)
//   }else{
//     // try {
//     //   // @ts-ignore
//     //   const babelUrl: string = window.BabelUrl || 'https://unpkg.com/@babel/standalone@7.26.4/babel.min.js';
//     //   const babelKey = "babel_"+hashId(babelUrl)
//     //   let babelCode = localStorage.getItem(babelKey)||"";
//     //   if (!babelCode){
//     //     babelCode = await fetch(babelUrl).then((res)=>res.text());
//     //     localStorage.setItem(babelKey,babelCode);
//     //   }
//     //   if (babelCode){
//     //     new Function(babelCode)();
//     //     // @ts-ignore
//     //     const babel: any = window.Babel || null;
//     //     if (babel){
//     //       code =  await babelTransform(ast,babel,source,moduleSourceType, additionalBabelPlugins, devMode, log)
//     //     }
//     //   }
//     // } catch (error){
//     //   console.log("error",error)
//     //   throw new Error(JSON.stringify(error, null,2))
//     // }
//     const msg = "缺少 @babel/standalone 依赖, https://unpkg.com/@babel/standalone@7.26.4/babel.min.js"
//     log?.('error', msg);
//     throw new Error(msg)
//   }
//   return {
//     code
//   };
// }


export async function babel_transformFromAstAsync(ast:any,source:string, moduleSourceType: boolean, additionalBabelPlugins: any = {}, devMode: boolean){
  return await transformFromAstAsync(ast, source, {
    sourceMaps: false,
    plugins: [ // https://babeljs.io/docs/en/options#plugins
      jsx,
      ...moduleSourceType ? [ babelPluginTransformModulesCommonjs ] : [], // https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs#options
      // @ts-ignore
      ...Object.values(targetBrowserBabelPlugins),
      ...additionalBabelPlugins !== undefined ? Object.values(additionalBabelPlugins) : [],
    ],
    babelrc: false,
    configFile: false,
    highlightCode: false,
    compact: !devMode, // doc: All optional newlines and whitespace will be omitted when generating code in compact mode.
    comments: devMode,
    retainLines: devMode,
    //envName: devMode ? 'development' : 'production', see 'process.env.BABEL_ENV': JSON.stringify(mode),
    //minified,
    sourceType: moduleSourceType ? 'module' : 'script',
  });
}
